<?php
namespace Modules\ShopApi\Models;
use Illuminate\Support\Facades\DB;
use Modules\ShopApi\Services\ShopUserService;
use Modules\ShopApi\Models\ShopOrder as ShopOrderModel;
class ShopOrderComplain extends BaseApiModel
{
	/**
	 * @name  订单申诉
	 * @return  JSON
	 */
	public function order_complain($data){
		$user_info = (new ShopUserService())->my();
		$status = ShopOrderModel::where(['id'=>$data['order_id'],'user_id'=>$user_info['id']])->value('status');
		if($status != 9 && $status != 7){
			return $this->api_error();
		};
		$images = [];
		foreach($data['images'] as $k=>$v){
			$images[] = $v['image_id'];
		}
		if(count($images)){
			$data['images'] = implode('|',$images);
		}else{
			unset($data['images']);
		}
		$data['user_id'] = $user_info['id'];
		$data['created_at'] = date('Y-m-d H:i:s');
		$data['status'] = 0;
		DB::beginTransaction();
		try{
			$this->insert($data);
			ShopOrderModel::where(['id'=>$data['order_id'],'user_id'=>$user_info['id']])->update([
				'status' => 6
			]);
			DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success();
	}
}
